<!DOCTYPE html>
<head>
    <title>商品详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css" /><!-- bootstrap -->
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script> <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <script type="text/javascript" src="/layer/layer.js"></script><!-- layer -->
    <script type="text/javascript" src="/js/md5.min.js"></script><!-- md5.js -->
    <script type="text/javascript" src="/js/common.js"></script><!-- common.js -->
    <script type="text/javascript" src="/js/socket.js"></script><!-- common.js -->
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
    <div id="userTip" style="display:none">
        <span> 您还没有登录，请<a href="/login.html">登陆</a>后再操作<br/></span>
    </div>
    <span>没有收货地址的提示。。。</span>
</div>
<table class="table">
    <tr>
        <td>商品名称</td>
        <td colspan="3" id="goodName"></td>
    </tr>
    <tr>
        <td>商品图片</td>
        <td colspan="3"><img id="goodImg"  width="200" height="200" /></td>
    </tr>
    <tr>
        <td>秒杀开始时间</td>
        <td id="startDate"></td>
        <td id="seckillTip">
        </td>
        <td>
            <img id="verifyCodeImg" width="80" height="32"  onclick="initVerifyCodeImg()" style="display: none">
            <input id="verifyCode" style="display: none">
            <button class="btn btn-primary btn-block"  type="button" disabled id="buyButton" onclick="getPath()">立即秒杀</button>
        </td>
    </tr>
    <tr>
        <td>商品原价</td>
        <td colspan="3" id="goodPrice"></td>
    </tr>
    <tr>
        <td>秒杀价</td>
        <td colspan="3" id="seckillPrice"></td>
    </tr>
    <tr>
        <td>库存数量</td>
        <td colspan="3" id="stockCount"></td>
    </tr>
</table>
</div>
<script>
    var seckillId;
    var uuid;
    $(function () {
        seckillId = getQueryString("seckillId");
        //用于是否登录
        renderUser()
        //渲染商品数据
        initSeckillGood(seckillId);


    })

    function getPath(){
        if(!user){
            layer.msg("请先登录")
            return
        }

        $.ajax({
            url:"http://localhost:9000/seckill/getPath",
            type:"POST",
            data:{"uuid":uuid,"verifyCode":$("#verifyCode").val()},
            xhrFields: { withCredentials: true },
            success:function(data){
                if(data.code==200){
                    doSeckill(data.data);

                    // window.location.href = "http://localhost/order_detail.html?orderNo=" + data.data
                }else{
                    layer.msg("错误码:"+data.code+"错误信息:"+data.msg)

                }
            }
        })

    }


    /* function getPath(){
         if(!user){
             layer.msg("请先登录")
             return
         }

         $.ajax({
             url:"http://localhost:9000/seckill/doSeckill?seckillId="+seckillId,
             type:"POST",
             //data:{uuid:uuid},
             xhrFields: { withCredentials: true },
             success:function(data){
                 if(data.code==200){
                     //doSeckill(data.data);
                     //initVerifyCode()
                     window.location.href = "http://localhost/order_detail.html?orderNo=" + data.data
                 }else{
                     layer.msg("错误码:"+data.code+"错误信息:"+data.msg)
                     //initVerifyCode()
                 }
             }
         })

     }*/

    var user;
    function renderUser(){
        $.ajax({
            url:"http://localhost:9000/member/getCurrent",
            type:"GET",
            xhrFields: { withCredentials: true },
            success:function(data){
                if(data.code==200){
                    //如果没有登录让用户去登录
                   showLogin(data.data)
                }else{
                    layer.msg("用户没有登录请先登录")                }
            }
        })
    }

    function doSeckill(path){
        if(!user){
            layer.msg("请先登录");
            return
        }

        $.ajax({
            url:"http://localhost:9000/seckill/"+path+"/doSeckill",
            //url:"http://localhost:9000/seckill/doSeckill?seckillId="+seckillId,
            type:"POST",
            data:{"seckillId":seckillId,"uuid":uuid},
            xhrFields: { withCredentials: true },
            success:function(data){
                if(data.code==200){
                    //和websocket建立联系
                    createScoket(uuid);
                    //Result {code:200 msg:操作成功 data:orderNo(订单编号) }
                    //window.location.href = "/order_detail.html?orderNo=" + data.data;
                    layer.msg(data.data);
                }else{
                    layer.msg("错误码:"+data.code+"错误信息:"+data.msg)
                }
            }
        })


    }

    function showLogin(u){
        user  = u;
        if(!u){
            $("#userTip").show();
        }else{
            uuid = getUuid(u.id)
             $("#verifyCodeImg").show();
             $("#verifyCode").show();
            //界面加载完初始化验证码

            initVerifyCodeImg()
        }
    }


    function initVerifyCodeImg(){
        $("#verifyCodeImg").prop("src","http://localhost:9000/seckill/initVerifyCode?uuid="+uuid+"&time="+new Date().getTime());
    }

    function initSeckillGood(seckillId){
        $.ajax({
            url:"http://localhost:9000/seckill/seckillGood/find?seckillId="+seckillId,
            type:"GET",
            xhrFields: { withCredentials: true },
            success:function(data){
                if(data.code==200){
                    render(data.data);

                    //渲染时间
                    renderDate(data.data.startDate,data.data.endDate);
                }else{
                    layer.msg("错误码:"+data.code+"错误信息:"+data.msg)
                }
            }
        })
    }
    var remainStartSeconds ;   //距离开始有多长时间
    var remainEndSeconds;    //距离结束有多长时间
    var timer;
    function renderDate(startDate,endDate){
        /**
         * startDate = 2020 -4 - 24  12:00
         * now  = 2020 - 4 - 24   11 : 30
         * startDate - now > 0      ======>距离秒杀开始还有多长时间
         * 秒杀状态 : 倒计时
         *
         * startDate = 2020 -4 - 24  12:00
         * now  = 2020 - 4 - 24   12:30
         * endDate = 2020 -4 - 24  13:00
         * startDate -  now  < 0
         * endDate - now > 0        ========> 距离秒杀结束还有多长时间
         * 秒杀状态  : 秒杀中
         *
         *
         * startDate = 2020 -4 - 24  12:00
         * now  = 2020 - 4 - 24   13:30
         * endDate = 2020 -4 - 24  13:00
         * startDate - now < 0
         * endDate - now < 0
         * 秒杀状态 : 秒杀已经结束了
         *
         */
        var startDate = new Date(startDate);
        var endDate = new Date(endDate);
        var now = new Date();
        remainStartSeconds = parseInt((startDate.getTime() - now.getTime())/1000);
        remainEndSeconds=parseInt((endDate.getTime()-now.getTime())/1000);

        timer = setInterval(showStatus,1000);
    }
    function showStatus(){
        remainStartSeconds --;
        remainEndSeconds --
        if(remainStartSeconds>0){
            //秒杀还未开始
            $("#seckillTip").html("距离秒杀开始还有"+remainStartSeconds+" 秒")
        }else if(remainEndSeconds>0){
            //秒杀中
            $("#seckillTip").html("距离秒杀结束还有"+remainEndSeconds+" 秒")
            //设置该按钮为可用可编辑
            $("#buyButton").prop("disabled",false);
        }else {
            //秒杀结束
            $("#seckillTip").html("秒杀结束");
            // $("#buyButton").prop("disabled",true);

            clearInterval(timer);
        }
    }
    function render(data){
        console.log(data)
        $("#goodName").html(data.goodName);
        $("#goodImg").prop("src",data.goodImg);
        $("#startDate").html(data.startDate);
        $("#goodPrice").html(data.goodPrice);
        $("#seckillPrice").html(data.seckillPrice);
        $("#stockCount").html(data.stockCount);
    }
</script>
</body>
</html>